/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> res;
    void func(TreeNode* root, int targetSum, vector<int> cnt_path)
    {
        if(!root)
            return;
        cnt_path.push_back(root->val);
        targetSum -= root->val;
        if(!root->left && !root->right)
        {
            if(targetSum == 0)
                res.push_back(cnt_path);
            return;
        }
        func(root->left, targetSum, cnt_path);
        func(root->right, targetSum, cnt_path);
    }
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        if(!root)
            return res;
        vector<int> cnt_path;
        func(root, targetSum, cnt_path);

        return res;
    }
};